package cn.tansanqinger.service.topic.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.account.UserAccountSqlDaoImpl;
import cn.tansanqinger.dao.sql.topic.QueryTopicSqlDaoImpl;
import cn.tansanqinger.dao.sql.topic.TopicSqlDaoImpl;
import cn.tansanqinger.pojo.Admin;
import cn.tansanqinger.pojo.Querytopic;
import cn.tansanqinger.pojo.Topic;
import cn.tansanqinger.service.topic.IQueryTopicService;
import cn.tansanqinger.util.Tools;

@Service
public class QueryTopicServiceImpl implements IQueryTopicService{

	@Autowired
	private QueryTopicSqlDaoImpl impl;
	@Autowired
	private UserAccountSqlDaoImpl uimpl;
	@Autowired
	private TopicSqlDaoImpl kimpl;

	@Override
	public List<Querytopic> selectTIdOne(String id) {
		List<Querytopic> list = impl.selectTid(Integer.parseInt(id));
		if(!list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {//名字
				list.get(i).setUname(uimpl.selectId(list.get(i).getUid()).getName());
			}
		}
		if (list.isEmpty()) {
			return null;
		}
		return list;
	}

	@Override
	public Querytopic selectIdOne(String id) {
		return impl.selectOnt(Integer.parseInt(id));
	}

	@Override
	public Map<String, Object> delete(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(id.matches("\\d+")) {
			if(impl.deleteOne(Integer.parseInt(id))==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "删除成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "删除失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据错误");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> modification(HttpServletRequest request) {
		Admin admin = (Admin) request.getSession().getAttribute("userMessage");
		String count = Tools.isEmpty(request.getParameter("count"));
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String message = Tools.isEmpty(request.getParameter("message"));
		if("1".equals(count)) { //表示全局处理
			List<Querytopic> list = selectTIdOne(Tools.isEmpty(request.getParameter("id")));
			if(list.isEmpty()) {
				resultMap.put("code","-1");
				resultMap.put("msg", "数据错误");
				return resultMap;
			}
			for (int i = 0; i < list.size(); i++) {
				Querytopic querytopic = list.get(i);
				querytopic.setQisdispose(1);
				querytopic.setAid(admin.getAid());
				querytopic.setQdisposemessage(message);
				impl.modification(querytopic);
			}
			//将知识数据重归所有
			Topic topic = kimpl.selectOnt(Integer.parseInt(Tools.isEmpty(request.getParameter("id"))));
			topic.setTisquery(0);
			kimpl.modification(topic);
			resultMap.put("code","0");
			resultMap.put("msg", "处理成功");
		} else {
			Querytopic querytopic = selectIdOne(Tools.isEmpty(request.getParameter("id")));
			querytopic.setQisdispose(1);
			querytopic.setQdisposemessage(message);
			querytopic.setAid(admin.getAid());
			if(impl.modification(querytopic)==1) {
				if(impl.selectTid(querytopic.getTid()).isEmpty()) {
					Topic topic = kimpl.selectOnt(querytopic.getTid());
					topic.setTisquery(0);
					kimpl.modification(topic);
				}
				resultMap.put("code","0");
				resultMap.put("msg", "处理成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "处理失败");
			}
		}
		return resultMap;
	}
}
